'use strict';
(function (exports) {
    //var cardSize = {w: 98, h: 122};//同城牌的size
    var cardSize = {w: 84, h: 104};
    var cardstr = 'card';
    var setSize = function (size) {
        cardSize = size;
    };
    var setCardStr = function (str) {
        cardstr = str;
    };
    var getCardImg = function (cardname) {
        if (cardname.indexOf('NaN') >= 0) return 'backbig.png';

        var src = cardname + '.png';
        //var src = "cardcommon_" + cardname + ".png";
        if (gameData.fontType == 'common') {
            src = 'cardcommon_' + cardname + '.png';
        } else if (gameData.fontType == 'ld') {
            src = cardname + '.png';
        }
        return src;
    };
    var Card = ccui.ImageView.extend({
        ctor: function () {
            this._super();

            cardstr = 'card';
            this.cardstr_storage = cc.sys.localStorage.getItem('setting_pai');
            if (this.cardstr_storage == 1 || !this.cardstr_storage) {
                cardSize = {w: 84, h: 104};
                gameData.fontType = 'deafullt';
                var imgSrc = this.getCardImg('mid_1');
                setUIImageFrameByName(this, imgSrc, cardstr);
            } else {
                cardSize = {w: 98, h: 122};
                gameData.fontType = 'common';
                var imgSrc = this.getCardImg('mid_1');
                cardstr = 'card_common';
                setUIImageFrameByName(this, imgSrc, cardstr);
            }


            //setUIImageFrameByName(this, imgSrc, 'card');
            //this.loadTexture("mid_1.png", ccui.Widget.PLIST_TEXTURE);
            this.isSelected = false;
            this.lastClickTime = 0;

            return this;
        },

        //type 0中 1大 2小
        setData: function (data, p, typ, isWei) {
            this.p = p;
            var visible = true;
            if (data == null) visible = false;
            data = parseInt(data);
            this.data = data;
            this.typ = typ;
            this.isWei = isWei;
            this.backBg = false;

            if (data < 0) {
                data = 0;
            } else if (data > 100) {
                data = 21;
            }

            var cardname = '';
            if (typ == 1) {
                cardname = 'big_' + data;
            } else if (typ == 2) {
                cardname = 'small_' + data;
            } else {
                cardname = 'mid_' + data;
            }
            //如果是0  显示牌背
            if (data == 0) {
                this.backBg = true;
                //var card = (typ == 1) ? "backbig.png" : "back.png";
                //this.loadTexture(card, ccui.Widget.PLIST_TEXTURE);
                //setUIImageFrameByName(this, card,"card");
                var i = cc.sys.localStorage.getItem('sceneid') || 0;
                if (typ == 1) {
                    this.loadTexture(huzi.resPath+'dynamicRes/' + bgArr[i][1] + '.png');
                } else {
                    this.loadTexture(huzi.resPath+'dynamicRes/' + bgArr[i][0] + '.png');
                }
            } else if (data >= 21) {
                //this.loadTexture("res/paohuzi/" + cardname + ".png", ccui.Widget.LOCAL_TEXTURE);
                setUIImageFrameByName(this, cardname + '.png', cardstr);
            } else {
                var imgSrc = this.getCardImg(cardname);
                if (typ == 2 && isWei == true) {
                    this.backBg = true;
                    var i = cc.sys.localStorage.getItem('sceneid') || 0;
                    this.loadTexture(huzi.resPath+'dynamicRes/' + bgArr[i][0] + '.png');
                } else {
                    this.imgSrc = imgSrc;
                    // this.loadTexture(imgSrc, ccui.Widget.PLIST_TEXTURE);
                    setUIImageFrameByName(this, imgSrc, cardstr);
                }

            }
            if (typ == 1) {
                //大牌加蓝边
                this.setContentSize(76, 226);
            } else if (typ == 2) {
                this.setScale(1.1);
            } else {
                this.setContentSize(cardSize.w, cardSize.h);
            }
            this.setVisible(visible);
        },

        setRowColumn: function (row, column) {
            this.row = row;
            this.column = column;
        },
        getRow: function () {
            return this.row;
        },
        getColumn: function () {
            return this.column;
        },

        setMo: function (flag) {
            var light = this.getChildByName('light');
            if (light == null) {
                light = new cc.Sprite(huzi.resPath+'dynamicRes/' + 'chupai_bj.png');
                light.setName('light');
                light.setPosition(cc.p(this.getContentSize().width / 2, this.getContentSize().height / 2));
                this.addChild(light, -1);
            }
            light.setPosition(cc.p(this.getContentSize().width / 2, this.getContentSize().height / 2));
            light.setVisible(true);
            light.setTexture(flag ? huzi.resPath+'dynamicRes/' + 'mopai_bj.png' : huzi.resPath+'dynamicRes/' + 'chupai_bj.png');
        },

        setSelectedCard: function (theCard) {
            this.selectedCard = theCard;

            this.setTouchEnabled(true);
            this.setSwallowTouches(true);
            if (mRoom.isReplay != true) {
                this.addTouchEventListener(this.onSelect, this);
            }
        },
        setTouchEnableStatus: function (flag) {
            if (this) {
                this.setTouchEnabled(flag);
            }
        },
        setGrayCard: function (flag) {
            var gray = this.getChildByName('gray');
            if (!gray) {
                gray = new cc.Scale9Sprite('res/paohuz/gray.png', cc.rect(0, 0, 50, 50), cc.rect(20, 20, 10, 10));
                gray.setContentSize(cc.size(this.getContentSize().width, this.getContentSize().height));
                gray.setPosition(cc.p(this.getContentSize().width / 2, this.getContentSize().height / 2));
                gray.setName('gray');
                this.addChild(gray);
            }
            gray.setVisible(flag);
        },

        /**
         * 给牌添加绿色图层（当前选择的牌，牌面上有的都变为绿色）
         * @param flag
         */
        setSelectCard: function (flag) {
            var select = this.getChildByName('select');
            if (!select) {
                select = new cc.LayerColor();
                select.setColor(cc.color(38, 160, 100));
                select.setOpacity(150);
                select.setContentSize(cc.size(this.getContentSize().width, this.getContentSize().height));
                select.setPosition(cc.p(0, 0));
                select.setName('select');
                this.addChild(select);
            }
            select.setVisible(flag);
        },
        /**
         * 给牌添加红色图层 （玩家吃的那张牌标红）
         */
        setChiCard: function () {
            var select = this.getChildByName('chi');
            if (!select) {
                select = new cc.LayerColor();
                select.setColor(cc.color(250, 0, 0));
                select.setOpacity(80);
                select.setContentSize(cc.size(this.getContentSize().width, this.getContentSize().height));
                select.setPosition(cc.p(0, 0));
                select.setName('chi');
                this.addChild(select);
            }
            select.setVisible(true);
        },
        getCardImg: function (cardname) {
            if (cardname.indexOf('NaN') >= 0) return 'backbig.png';

            var src = cardname + '.png';
            //var src = "cardcommon_" + cardname + ".png";
            if (gameData.fontType == 'common') {
                src = 'cardcommon_' + cardname + '.png';
            } else if (gameData.fontType == 'ld') {
                src = cardname + '.png';
            }
            return src;
        },
        resetCardImg: function () {
            var typ = this.typ;
            var data = this.data;
            var cardname = '';
            if (typ == 1) {
                cardname = 'big_' + data;
            } else if (typ == 2) {
                cardname = 'small_' + data;
            } else {
                cardname = 'mid_' + data;
            }
            //如果是0  显示牌背
            if (this.data == 0) {
                this.backBg = true;
                // var card = "backbig.png";
                // // this.loadTexture(card, ccui.Widget.PLIST_TEXTURE);
                // setUIImageFrameByName(this, card, 'card_common');
                var i = cc.sys.localStorage.getItem('sceneid') || 0;
                if (typ == 1) {
                    this.loadTexture(huzi.resPath+'dynamicRes/' + bgArr[i][1] + '.png');
                } else {
                    this.loadTexture(huzi.resPath+'dynamicRes/' + bgArr[i][0] + '.png');
                }
            } else {
                var imgSrc = this.getCardImg(cardname);
                // console.log("imgSrc"+imgSrc);
                if (this.typ == 2 && this.isWei == true) {
                    this.backBg = true;
                    imgSrc = 'back.png';
                    var i = cc.sys.localStorage.getItem('sceneid') || 0;
                    if (typ == 1) {
                        this.loadTexture(huzi.resPath+'dynamicRes/' + bgArr[i][1] + '.png');
                    } else {
                        this.loadTexture(huzi.resPath+'dynamicRes/' + bgArr[i][0] + '.png');
                    }
                } else {
                    // this.loadTexture(imgSrc, ccui.Widget.PLIST_TEXTURE);
                    setUIImageFrameByName(this, imgSrc, cardstr);
                }
            }
        },

        resetCardBackImg: function (bgName) {
            if (!this.backBg) {
                return;
            }
            var card = bgName + '.png';
            this.loadTexture(huzi.resPath+'dynamicRes/' + card);
        },
        onStatusChange: function (event) {
            if (this.checkNoRunning()) return;
            var data = event.getUserData();

        },

        onSelect: function (sender, type) {
            switch (type) {
                case ccui.Widget.TOUCH_BEGAN:
                    this.isSelected = true;
                    this.setOpacity(180);
                    this.selectedCard.setData(this.data);
                    var pos = sender.getTouchBeganPosition();
                    this.updateSelectedCard(true, pos);

                    this.p.setSameCardShow(this.data, true);
                    break;

                case ccui.Widget.TOUCH_MOVED:
                    var pos = sender.getTouchMovePosition();
                    this.updateSelectedCard(true, pos);
                    break;

                case ccui.Widget.TOUCH_ENDED:
                    this.isSelected = false;
                    this.setOpacity(255);
                    var pos = sender.getTouchEndPosition();

                    // var clickTime = Date.now();
                    // if(clickTime - this.lastClickTime < 500){
                    //     this.lastClickTime = 0;
                    //     this.showCard();
                    //     this.selectedCard.setVisible(false);
                    //     return;
                    // }

                    this.lastClickTime = Date.now();
                    this.updateSelectedCard(false, pos);

                    this.p.setSameCardShow(this.data, false);
                    break;

                case ccui.Widget.TOUCH_CANCELED:
                    this.isSelected = false;
                    this.setOpacity(255);
                    var pos = sender.getTouchEndPosition();
                    this.updateSelectedCard(false, pos);

                    this.p.setSameCardShow(this.data, false);
                    break;

                default:
                    break;
            }
            return false;
        },

        updateSelectedCard: function (isShow, pos) {
            this.selectedCard.setVisible(isShow);
            var px = pos.x;
            var py = pos.y;
            this.selectedCard.setPosition(px, py);

            if (isShow == false) {
                if (py > 480)
                    this.showCard();
                else {
                    this.p.moveCard(this, px, py);
                }
            }
        },

        showCard: function () {
            if (this.p.isTurnOut == true) {
                this.p.userShowCard(this.data, false, this.row, this.column);
            } else {
                // HUD.showMessage("未到出牌回合！");
            }
        },
        setOutCardray: function () {
            var select = this.getChildByName('out');
            if (!select) {
                select = new cc.LayerColor();
                select.setColor(cc.color(75, 75, 75));
                select.setOpacity(80);
                select.setContentSize(cc.size(this.getContentSize().width, this.getContentSize().height));
                select.setPosition(cc.p(0, 0));
                this.addChild(select);
            }
            select.setVisible(true);

        },
        setCardGray: function (flag) {
            var select = this.getChildByName('graycard');
            if (!select) {
                select = new cc.LayerColor();
                select.setColor(cc.color(75, 75, 75));
                select.setOpacity(80);
                select.setContentSize(cc.size(this.getContentSize().width, this.getContentSize().height));
                select.setPosition(cc.p(0, 0));
                select.setName('graycard');
                this.addChild(select);
            }
            select.setVisible(flag);

        },
        setKan: function (isKan) {
            this.isKan = isKan;
        },

        setTingMark: function (flag) {
            var mark = this.getChildByName('tingmark');
            if (!mark) {
                mark = new cc.Sprite(res.tingmark_png);
                mark.setPosition(98 * .9 / 6, 122 * .9 - 20);
                mark.setName('tingmark');
                // mark.setColor(cc.color(180,180,180));
                this.addChild(mark);
            }
            mark.setVisible(flag);
        },

    });
    exports.Card = Card;
    exports.Card.setSize = setSize;
    exports.Card.setCardStr = setCardStr;
    exports.Card.getCardImg = getCardImg;
})(window);